package com.cug.nageoffer.user.controller;

import com.cug.nageoffer.model.entity.User;
import com.cug.nageoffer.model.vo.LoginVo;
import com.cug.nageoffer.model.vo.Result;
import com.cug.nageoffer.model.vo.ResultCodeEnum;
import com.cug.nageoffer.model.vo.UserVo;
import com.cug.nageoffer.user.service.UserService;
import com.cug.nageoffer.user.util.ElasticSearchUtil;
import com.cug.nageoffer.util.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@Tag(name = "前台用户相关")
@RestController
@RequestMapping("/api/seeker/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private ElasticSearchUtil elasticSearchUtil;

    /**
     * 用户注册接口
     */
    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result<String> register(@RequestBody User userDO){
        return userService.register(userDO);
    }

    /**
     * 用户登录接口
     */
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody User userDO){
        log.error("用户{}登录",userDO);
        return Result.build(userService.login(userDO),ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户退出接口
     */
    @Operation(summary = "用户退出")
    @PostMapping("/logout")
    public Result<Boolean> logout(@RequestBody User userDO){
        return Result.build(userService.logout(userDO),ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询用户信息接口
     */
    @Operation(summary = "查询用户信息")
    @GetMapping("/queryInfo")
    public Result<User> queryInfo(@RequestParam String username){
        return Result.build(userService.queryInfo(username),ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询用户自己的信息接口
     */
    @Operation(summary = "查询用户自己的信息")
    @GetMapping("/querySelfInfo")
    public Result<User> querySelfInfo(){
        String username = AuthContextUtil.get().getUsername();
        log.info("获取到用户名");
        return Result.build(userService.queryInfo(username),ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改用户信息接口
     */
    @Operation(summary = "修改用户信息")
    @PostMapping("/modifyInfo")
    public Result<String> modifyInfo(@RequestBody User userDO){
        return userService.modifyInfo(userDO);
    }

    /**
     * 修改用户求职状态接口
     */
    @Operation(summary = "修改用户求职状态")
    @PostMapping("/modifyStatus")
    public Result<String> modifyStatus(@RequestParam Integer status){
        log.info("修改用户求职状态:{}",status);
        return userService.modifyStatus(status);
    }

    /**
     * 上传用户头像
     */
    @Operation(summary = "上传用户头像")
    @PostMapping("/uploadProfile")
    public Result<String> uploadProfile(@RequestParam(value = "file") MultipartFile multipartFile) throws Exception {
        return userService.uploadProfile(multipartFile);
    }

    /**
     * 搜索用户
     */
    @Operation(summary = "搜索用户")
    @PostMapping("/searchByMatch")
    public Result<List<UserVo>> searchByMatch(@RequestParam String value) throws IOException {
        return Result.build(elasticSearchUtil.getUserByMatch("user","username",value),ResultCodeEnum.SUCCESS);
    }
}
